package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class UsercouponController {
    @Autowired
    private UserService userService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private UsercouponService usercouponService;
    @ModelAttribute("userMap")
    public Map<Integer, String> getUserMap() {
        List<User> users = userService.findAll();
        Map<Integer, String> userMap = new HashMap<>();
        for (User user : users) {
            userMap.put(user.getUserId(), user.getUserName());
        }
        return userMap;
    }

    @ModelAttribute("couponMap")
    public Map<Integer, String> getCouponMap() {
        List<Coupon> coupons = couponService.findAll();
        Map<Integer, String> couponMap = new HashMap<>();
        for (Coupon coupon : coupons) {
            couponMap.put(coupon.getCouponId(), coupon.getCouponName());
        }
        return couponMap;
    }

    @GetMapping("/usercoupon/{id}")
    @ResponseBody
    public ResponseEntity<Usercoupon> getUsercouponById(@PathVariable("id") Integer usercouponId) {
        Usercoupon usercoupon = usercouponService.queryById(usercouponId);
        if (usercoupon == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(usercoupon, HttpStatus.OK);
    }

    @PutMapping("/usercoupon/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateUsercoupon(@PathVariable("id") Integer usercouponId, @RequestBody Usercoupon usercoupon) {
        try {
            usercoupon.setCouponId(usercouponId);
            usercouponService.update(usercoupon);
            return ResponseEntity.ok(Map.of("success", true, "message", "用户优惠券信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/usercoupon/{id}")
    public ResponseEntity<Map<String, Object>> deleteUsercoupon(@PathVariable("id") Integer usercouponId) {
        try {
            boolean isDeleted = usercouponService.deleteById(usercouponId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "用户优惠券信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "用户优惠券信息删除失败，用户优惠券不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(Map.of("success", false, "message", "用户优惠券信息删除失败: " + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/usercoupon")
    @ResponseBody
    public String createUsercoupon(Usercoupon usercoupon) {
        try {
            usercouponService.insert(usercoupon);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/usercoupon")
    public String listUsercoupon(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Usercoupon usercoupon) {
        usercoupon.setStart((page - 1) * rows);
        usercoupon.setRows(rows);
        Page<Usercoupon> page11 = usercouponService.selectAll(usercoupon);
        page11.setPage(page);
        model.addAttribute("page", page11);
        return "usercoupon";
    }

}


